<?php defined('SYSPATH') OR die('No direct access allowed.');

class Render_Jsonp extends Render {

	public static function render($response, array $vars=array(), array $globals=array(), $file=false, array $options=array())
	{
		if ( empty($vars['_no_header']) && ($response instanceof Response) ) {
			$response->headers('Content-Type', 'application/x-javascript; charset='.Kohana::$charset);
		}

		$data = isset($vars['_data']) ? $vars['_data'] : null;
		$jsonp = !empty($vars['_jsonp']) ? $vars['_jsonp'] : 'callback';
		$jsonp = htmlspecialchars($jsonp);
		$result = json_encode($data);
		return 'if (typeof '.$jsonp.' == "Function") {'.$jsonp.'.call(this,'.$result.');}';
	}

}
